package com.gc.web.controller.api;

import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.MemoNote;
import com.gc.business.service.IMemoNoteService;
import com.gc.common.constant.Constants;
import com.gc.framework.web.base.ApiBaseController;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 备忘录 控制层
 *
 * @author liujinteng
 * @date 2020-12-02
 */
@Slf4j
@Controller
@RequestMapping("/api/memoNote")
public class ApiMemoNoteController extends ApiBaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IMemoNoteService memoNoteService;

    /**
     * 查询备忘录列表
     */
    @GetMapping("/selectMemoNoteList")
    @ResponseBody
    public Map<String, Object> selectMemoNoteList(String token,MemoNote memoNote,Integer pageNum,Integer pageSize) {
        Long userId = getUserId(token);
        startPage(pageNum,pageSize,"create_time DESC");
        memoNote.setUserId(userId);
        List<MemoNote> memoNoteList = memoNoteService.list(memoNote);
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("list",memoNoteList);
        map.put("total",new PageInfo<>(memoNoteList).getTotal());
        return success(map);
    }

    /**
     * 添加备忘录
     */
    @PostMapping("/saveMemoNote")
    @ResponseBody
    public Map<String, Object> saveMemoNote(String token,MemoNote memoNote) {
        SysUser user = userService.selectUserById(getUserId(token));
        memoNote.setUserId(user.getUserId());
        boolean res = false;
        if (memoNote.getMemoId() == null){
            memoNote.setCreateBy(user.getUserName());
            memoNote.setCreateTime(DateUtil.date());
            res = memoNoteService.save(memoNote);
        }else {
            memoNote.setUpdateBy(user.getUserName());
            memoNote.setUpdateTime(DateUtil.date());
            res = memoNoteService.updateById(memoNote);
        }
        if (res){
            return success(memoNote);
        }
        return error(Constants.UNVERIFY,"保存失败");
    }

    /**
     * 删除接口
     */
    @PostMapping("/deleteMemoNote")
    @ResponseBody
    public Map<String, Object> deleteMemoNote(Long memoId) {
        if (memoId == null){
            return error(Constants.UNVERIFY,"参数错误:");
        }
        boolean b = memoNoteService.removeById(memoId);
        if (b){
            return success("删除成功");
        }
        return error(Constants.UNVERIFY,"删除失败");
    }
    /**
     * 详情接口
     */
    @PostMapping("/memoNoteDetail")
    @ResponseBody
    public Map<String, Object> memoNoteDetail(Long memoId) {
        if (memoId == null){
            return error(Constants.UNVERIFY,"参数错误:");
        }
        MemoNote memoNote = memoNoteService.getById(memoId);
        if (memoNote == null){
            return error(Constants.UNVERIFY,"该数据不存在");
        }
        return success(memoNote);
    }
}
